package com.tdx.blog_nowcoder.myconfig;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.resource.DefaultClientResources;
//import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;


/**
 /**
 * @author tdx
 * @version 1.0
 * @description: TODO
 * @date 2022/8/4 16:36

 **/
//@Configuration
public class RedisConfig extends CachingConfigurerSupport {

//    @Resource
//    private RedisProperties redisProperties;
//
//    public GenericObjectPoolConfig<?> genericObjectPoolConfig(RedisProperties.Pool properties) {
//        GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
//        config.setMaxTotal(properties.getMaxActive());
//        config.setMaxIdle(properties.getMaxIdle());
//        config.setMinIdle(properties.getMinIdle());
//        if (properties.getTimeBetweenEvictionRuns() != null) {
//            config.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRuns().toMillis());
//        }
//        if (properties.getMaxWait() != null) {
//            config.setMaxWaitMillis(properties.getMaxWait().toMillis());
//        }
//        return config;
//    }
//
//    @Bean
//    public DefaultClientResources lettuceClientResources() {
//        return DefaultClientResources.create();
//    }
//
////    @Bean(destroyMethod = "destroy")
////    public LettuceConnectionFactory lettuceConnectionFactory(ClientResources clientResources) {
////
////        //开启 自适应集群拓扑刷新和周期拓扑刷新
////        ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
////                // 开启全部自适应刷新
////                .enableAllAdaptiveRefreshTriggers() // 开启自适应刷新,自适应刷新不开启,Redis集群变更时将会导致连接异常
////                // 自适应刷新超时时间(默认30秒)
////                //默认关闭开启后时间为30秒
////                .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(15))
////                // 开周期刷新
////                // 默认关闭开启后时间为60秒 ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD 60  .enablePeriodicRefresh(Duration.ofSeconds(2)) = .enablePeriodicRefresh().refreshPeriod(Duration.ofSeconds(2))
////                .enablePeriodicRefresh(Duration.ofSeconds(15))
////                .build();
////
////        // https://github.com/lettuce-io/lettuce-core/wiki/Client-Options
////        ClientOptions clientOptions = ClusterClientOptions.builder()
////                .topologyRefreshOptions(clusterTopologyRefreshOptions)
////                .build();
////
////        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
////                .clientResources(clientResources)
////                .poolConfig(genericObjectPoolConfig(redisProperties.getLettuce().getPool()))
////                .readFrom(ReadFrom.MASTER_PREFERRED)
////                .clientOptions(clientOptions)
////                //默认RedisURI.DEFAULT_TIMEOUT 60
////                .commandTimeout(redisProperties.getTimeout())
////                .build();
////
////        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
////        clusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
////        // clusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
////
////        return new LettuceConnectionFactory(clusterConfiguration, clientConfig);
////    }
//
//    @Bean
//    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
//        RedisTemplate<Object, Object> redisTemplate=new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//
//        // json序列化设置
//        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer
//                = new Jackson2JsonRedisSerializer<>(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//
//        om.activateDefaultTyping(om.getPolymorphicTypeValidator(),
//                ObjectMapper.DefaultTyping.NON_FINAL,//类名序列化到json串中
//                JsonTypeInfo.As.WRAPPER_ARRAY);
//
//
//
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//
//        //String类型的序列化
//        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
//
//        redisTemplate.setKeySerializer(stringSerializer);// key采用String序列化方式
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
//        redisTemplate.setHashKeySerializer(stringSerializer);// Hash key采用String序列化方式
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }

}
